package com.cxs.xiaomi.dao.impl;

import com.cxs.xiaomi.dao.UserDao;
import com.cxs.xiaomi.domain.User;
import cxs.utils.handler.impl.ResultHandlerAsBeanImpl;
import cxs.utils.handler.impl.ResultHandlerAsListImpl;
import cxs.utils.templete.JdbcTemplete;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  17:56
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class UserDaoImpl implements UserDao {
    @Override
    public List<User> selectByUsername(String username) {
        String sql = "select * from tb_user where username = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsListImpl<>(User.class),username);
    }

    @Override
    public int insert(User user) {
        String sql = "insert into tb_user values(null,?,?,?,?,0,1,?)";
        Object[] arrs = {
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getGender(),
                user.getCode()};
        return JdbcTemplete.DMLTemplete(sql,arrs);
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql = "select * from tb_user where username = ? and password = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsBeanImpl<>(User.class),username,password);
    }

    @Override
    public User selectById(int userId) {
        String sql = "select * from tb_user where id = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsBeanImpl<>(User.class),userId);
    }

    @Override
    public User selectByEmailAndCode(String email, String code) {
        String sql = "select * from tb_user where email = ? and code = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsBeanImpl<>(User.class),email,code);
    }

    @Override
    public int updateCodeById(Integer id, Integer i) {
        String sql = "update tb_user set flag = ? where id = ?";
        return JdbcTemplete.DMLTemplete(sql,i,id);
    }

    @Override
    public List<User> selectAll() {
        String sql = "select * from tb_user";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsListImpl<>(User.class));
    }

    @Override
    public int deleteById(int id) {
        String sql = "delete from tb_user where id = ?";
        return JdbcTemplete.DMLTemplete(sql,id);
    }

    @Override
    public List<User> selectByUsernameAndGender(String condition, List params) {
        String sql = "select *from tb_user " + condition;
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsListImpl<>(User.class),params.toArray());
    }
}
